﻿using System;
using System.Collections.Generic;
using BLL;
using Utils;
namespace UI.ModMini
{
    public partial class HostelEmpFee : System.Web.UI.Page
    {
        DefDicBiz defDicBiz = new DefDicBiz();
        MiniBiz miniBiz = new MiniBiz();
        string empCode = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["EmpCode"] != null)
            {
                empCode = Request.Params["EmpCode"].ToString();
            }
            //else
            //{
            //    JSUtility.Alert(this, "参数错误");
            //    return;
            //}
            btnIn.Attributes.Add("onclick", "javascript:ShowLink('HostelEmpFeeImport.aspx',800,480);");
            if (!IsPostBack)
            {

                defDicBiz.InitXMLDropDownList(ddlYear, "YearList.xml", System.DateTime.Now.Year.ToString());
                defDicBiz.InitXMLDropDownList(ddlMonth, "MonthList.xml", (System.DateTime.Now.Month - 1).ToString());
                if (!string.IsNullOrWhiteSpace(empCode))
                {
                    txtEmpCode.Text = empCode;
                    BondData(System.DateTime.Now.Year.ToString(), System.DateTime.Now.AddMonths(-1).Month.ToString());
                }

            }
        }
        protected void BondData(string strYear, string strMonth)
        {
            gvList.DataSource = miniBiz.GetEmpFei(strYear, strMonth);
            gvList.DataBind();
        }
        //只能操作本月或上一月
        bool CheckYearMonth(string strYear, string strMonth)
        {
            if (strYear == System.DateTime.Now.Year.ToString() &&((System.DateTime.Now.AddMonths(-1).Month.ToString() == strMonth) || (System.DateTime.Now.Month.ToString() == strMonth)))
                return true;         
            else
                return false;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            if (!CheckYearMonth(strYear, strMonth))
            {
                JSUtility.Alert(this, "年份或月份不对");
                return;
            }
            empCode = txtEmpCode.Text.Trim();
            if (miniBiz.IsExistFei(empCode, strYear, strMonth))
            {
                JSUtility.Alert(this, "该年月住宿费用已经存在");
                return;
            }
           
            if (!miniBiz.IsInHostel(empCode))
            {
                JSUtility.Alert(this, "该员工还没有入住宿舍");
                return;
            }
            string fei = txtFei.Text.Trim();
            if (!RegexRule.IsDecimal(fei))
            {
                JSUtility.Alert(this, "住宿费用必须填入数字");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("EmpCode", empCode);
            sdList.Add("FYear", strYear);
            sdList.Add("FMonth", strMonth);
            sdList.Add("Fei", fei);
            string info = string.Empty;
            if (miniBiz.Insert("Mini_HostelEmpFee", sdList))
            {
                info = "增加信息成功";
                BondData(strYear, strMonth);
            }
            else
                info = "增加信息失败";
            JSUtility.Alert(this, info);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string id = hidID.Value;
            if (string.IsNullOrWhiteSpace(id) || txtEmpCode.Text == "")
            {
                //弹出提示窗口
                JSUtility.Alert(this, "ID和工号不能为空！");
                return;
            }
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            if (!CheckYearMonth(strYear, strMonth))
            {
                JSUtility.Alert(this, "年份或月份不对");
                return;
            }
            empCode = txtEmpCode.Text.Trim();
            if (!miniBiz.IsExistFei(empCode, strYear, strMonth))
            {
                JSUtility.Alert(this, "该年月住宿费用不存在");
                return;
            }
           
            if (!miniBiz.IsInHostel(empCode))
            {
                JSUtility.Alert(this, "该员工还没有入住宿舍");
                return;
            }
            string fei = txtFei.Text.Trim();
            if (!RegexRule.IsDecimal(fei))
            {
                JSUtility.Alert(this, "住宿费用必须填入数字");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ID", id);
            sdList.Add("EmpCode", empCode);
            sdList.Add("FYear", strYear);
            sdList.Add("FMonth", strMonth);
            sdList.Add("Fei", fei);
            string info = string.Empty;
            if (miniBiz.Update("Mini_HostelEmpFee", sdList))
            {
                info = "修改成功";
                BondData(strYear, strMonth);
            }
            else
                info = "修改信息失败";
            JSUtility.Alert(this, info);
        }
        string info = string.Empty;
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            if (!CheckYearMonth(strYear, strMonth))
            {
                JSUtility.Alert(this, "年份或月份不对");
                return;
            }
            string id = hidID.Value;
            if (string.IsNullOrWhiteSpace(id) || txtEmpCode.Text == "")
            {
                //弹出提示窗口
                JSUtility.Alert(this, "ID和工号不能为空！");
                return;
            }
            //new string[] { "ID", id, "N" }
            string info = miniBiz.Delete("Mini_HostelEmpFee", new string[] { "ID", id, "N" });
            BondData(strYear, strMonth);

            JSUtility.Alert(this, info);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            BondData(strYear, strMonth);
        }
    }
}